﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System.Collections.Generic;
using Sebarf.Services.Interfaces;
using Sebastof.Services.Interfaces;
using Sebastof.Types;

#endregion

namespace Sebastof.Client.Services.Impl {
	/// <summary>
	/// Impl of <see cref="IObjectTrackerMetadataService"/>
	/// </summary>
	public class ObjectTrackerMetadataService : Service, IObjectTrackerMetadataService {
		#region Public Properties

		[ServiceRequest]
		public IMetadataLocatorService MetaDataLocatorService { get; set; }

		[ServiceRequest]
		public IChangeTrackingStorageService ChangeTrackingService { get; set; }

		#endregion

		#region Public Methods

		public ObjectTrackerMetadataService(IMetadataLocatorService metaDataLocatorService) {
			MetaDataLocatorService = metaDataLocatorService;
		}

		public ObjectTrackerMetadataService() {
		}

		#endregion

		#region IObjectTrackerMetadataService Members

		public EChangeState GetChangeState(object obj) {
			return MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangedState", EChangeState.New);
		}

		public void SetChangeState(object obj, EChangeState state) {
			MetaDataLocatorService.SetMetadata(obj, "ObjectTrackerMetadataService.ChangedState", state);
		}

		public ChangeSet[] GetChangeSet(object obj) {
			return
				MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangeSets", new List<ChangeSet>()).ToArray
					();
		}

		public ChangeCollectionSet[] GetChangeCollectionSet(object obj) {
			return
				MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangeCollectionSets",
													new List<ChangeCollectionSet>()).ToArray();
		}

		public void SetChangeSet(object obj, ChangeSet[] changeSets) {
			MetaDataLocatorService.SetMetadata(obj, "ObjectTrackerMetadataService.ChangeSets", new List<ChangeSet>(changeSets));
		}

		public void AddChangeSet(object obj, ChangeSet changeSet) {
			MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangeSets", new List<ChangeSet>()).Add(
				changeSet);
		}

		public void AddChangeSet(object obj, ChangeCollectionSet changeSet) {
			MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangeCollectionSets",
												new List<ChangeCollectionSet>()).Add(changeSet);
		}

		public void AddChangeSet(ChangeSet changeSet) {
			ChangeTrackingService.CurrentDepositedChangesOfNonFullyLoadedObjects.Add(changeSet);
		}

		public void RemoveChangeSets(object obj) {
			List<ChangeSet> a = MetaDataLocatorService.GetMetadata(obj, "ObjectTrackerMetadataService.ChangeSets",
																	new List<ChangeSet>());
			a.Clear();
		}

		#endregion
	}
}